package com.springboot.cloud.demos.producer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.cloud.demos.entity.po.MemberCard;
import com.springboot.cloud.demos.producer.dao.MemberCardMapper;
import com.springboot.cloud.demos.entity.param.MemberCardQueryParam;
import com.springboot.cloud.demos.producer.service.IMemberCardService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
@RefreshScope
public class MemberCardServiceImpl extends ServiceImpl<MemberCardMapper, MemberCard> implements IMemberCardService {

    @Value("${producer.memberCard:123}")
    private String value;

    @Override
    @CacheEvict(value = "memberCard", allEntries = true)
    public boolean add(MemberCard memberCard) {
        return this.save(memberCard);
    }

    @Override
    @CacheEvict(value = "memberCard", key = "#root.targetClass+'-'+#id")
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @CacheEvict(value = "memberCard", key = "#root.targetClass+'-'+#memberCard.id")
    public boolean update(MemberCard memberCard) {
        return this.updateById(memberCard);
    }

    @Override
    @Cacheable(value = "memberCard", key = "#root.targetClass+'-'+#id")
    public MemberCard get(String id) {
        log.info("value:{}", value);
        return this.getById(id);
    }

    @Override
    @Cacheable(value = "memberCard", key = "#root.targetClass+'-list'+#memberCardQueryParam.type")
    public List<MemberCard> query(MemberCardQueryParam memberCardQueryParam) {
        QueryWrapper<MemberCard> queryWrapper = memberCardQueryParam.build();
        if (StringUtils.isNotEmpty(memberCardQueryParam.getDeleted())) {
            queryWrapper.eq("deleted", memberCardQueryParam.getDeleted());
        }
        if (StringUtils.isNotEmpty(memberCardQueryParam.getType())) {
            queryWrapper.eq("type", memberCardQueryParam.getType());
        }
        queryWrapper.orderByDesc("created_time");
        return this.list(queryWrapper);
    }
}
